﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;
using System.Data;

namespace SD.Web.Controls
{
    public partial class DirectorateList : System.Web.UI.UserControl
    {
        /// <summary>
        /// Maintain the filter value
        /// </summary>
        public string Filter
        {
            get
            {
                if (ViewState["Filter"] != null)
                {
                    return (string)this.ViewState["Filter"];
                }
                else
                {
                    return "0123456789qwertyuiopasdfghjklzxcvbnm";
                }
            }

            set
            {
                this.ViewState["Filter"] = value;
            }
        }


        /// <summary>
        /// Maintain the sortby value (ex: abcd, efgh)
        /// </summary>
        public string SortBy
        {
            get
            {
                if (ViewState["SortBy"] != null)
                {
                    return (string)this.ViewState["SortBy"];
                }
                else
                {
                    return "DirectorateName";
                }
            }

            set
            {
                this.ViewState["SortBy"] = value;
            }
        }
        /// <summary>
        /// Maintain the sortType value (a->z or z->a)
        /// </summary>
        public int SortType
        {
            get
            {
                if (ViewState["sortType"] != null)
                {
                    return (int)this.ViewState["sortType"];
                }
                else
                {
                    this.ViewState["sortType"] = 0;
                    return (int)this.ViewState["sortType"];
                }
            }

            set
            {
                this.ViewState["sortType"] = value;
            }
        }

        /// <summary>
        /// Maintain the total pages of datagrid
        /// </summary>
        public int PageCount
        {
            get
            {
                return Convert.ToInt16(this.ViewState["PageCount"].ToString());
            }

            set
            {
                this.ViewState["PageCount"] = value;
            }
        }

        /// <summary>
        /// flag of sortType
        /// </summary>
        public int Flag
        {
            get
            {
                if (ViewState["Flag"] != null)
                {
                    return (int)this.ViewState["Flag"];
                }
                else
                {
                    int flag = 0;
                    this.ViewState["Flag"] = flag;
                    return (int)this.ViewState["Flag"];
                }
            }

            set
            {
                this.ViewState["Flag"] = value;
            }
        }

        /// <summary>
        /// Maintain the current page
        /// </summary>
        public int CurPage
        {
            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 1;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }

            set
            {
                this.ViewState["CurrentPage"] = value;
            }
        }

        /// <summary>
        /// page load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData(Filter, SortBy, SortType);
            }
        }

        /// <summary>
        /// Fill the data to the form
        /// </summary>
        /// <param name="filter">type of filter</param>
        /// <param name="sortType">type of sort</param>
        public void LoadData(string filter, string sortBy, int sortType)
        {
            List<StoredDirectorateResult> directorate = new DirectorateBUS().ListDirectorate((!CheckBoxInactive.Checked), filter, sortBy, sortType);
            //total page
            PageCount = (int)Math.Ceiling(directorate.Count / 15.0);
            if (PageCount == 0)
            {
                PageCount = 1;
            }

            //check link button to invisible or visible
            if (CurPage == 1)
            {
                btnFirst.Visible = false;
                btnPrevious.Visible = false;
                btnLast.Visible = true;
                btnNext.Visible = true;
            }
            if (CurPage == PageCount && PageCount == 1)
            {
                btnFirst.Visible = false;
                btnPrevious.Visible = false;
                btnLast.Visible = false;
                btnNext.Visible = false;
            }
            else
            {
                if (CurPage == PageCount)
                {
                    btnFirst.Visible = true;
                    btnPrevious.Visible = true;
                    btnLast.Visible = false;
                    btnNext.Visible = false;
                }
            }
            if (CurPage > 1 && CurPage < PageCount)
            {
                btnFirst.Visible = true;
                btnPrevious.Visible = true;
                btnLast.Visible = true;
                btnNext.Visible = true;
            }
            //show on grid view
            GridViewDirectorate.DataSource = directorate.Skip((CurPage - 1) * 15).Take(15);
            GridViewDirectorate.DataBind();
        }

        /// <summary>
        /// Event handles the filter action
        /// </summary>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            //sort by
            CurPage = 0;
            Filter = e.CommandName;
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// checkbox checked changed event
        /// </summary>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// Paging
        /// </summary>
        protected void btnLast_Click(object sender, EventArgs e)
        {
            CurPage = PageCount;
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// Paging
        /// </summary>
        protected void btnFirst_Click(object sender, EventArgs e)
        {
            CurPage = 1;
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// Paging
        /// </summary>
        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            CurPage -= 1;
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// Paging
        /// </summary>
        protected void btnNext_Click(object sender, EventArgs e)
        {
            CurPage += 1;
            LoadData(Filter, SortBy, SortType);
        }

        /// <summary>
        /// Handle the sorting action
        /// </summary>
        protected void GridViewDirectorate_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sort = e.SortExpression;
            //sort program name
            if (sort == "DirectorateName")
            {
                if (Flag == 0)
                {
                    SortType++;
                }
                else
                {
                    SortType = 0;
                    Flag = 0;
                }
                SortBy = "DirectorateName";
                LoadData(Filter, SortBy, SortType);
            }
            //sort description
            if (sort == "Address1")
            {
                if (Flag == 1)
                {
                    SortType++;
                }
                else
                {
                    SortType = 0;
                    Flag = 1;
                }
                SortBy = "AddressLine";
                LoadData(Filter, SortBy, SortType);
            }
            //sort contact name
            if (sort == "PostCode")
            {
                if (Flag == 2)
                {
                    SortType++;
                }
                else
                {
                    SortType = 0;
                    Flag = 2;
                }
                SortBy = "PostCode";
                LoadData(Filter, SortBy, SortType);
            }
            //sort is active
            if (sort == "SurName")
            {
                if (Flag == 3)
                {
                    SortType++;
                }
                else
                {
                    SortType = 0;
                    Flag = 3;
                }
                SortBy = "SurName";
                LoadData(Filter, SortBy, SortType);
            }
            //sort is active
            if (sort == "IsActive")
            {
                if (Flag == 4)
                {
                    SortType++;
                }
                else
                {
                    SortType = 0;
                    Flag = 4;
                }
                SortBy = "IsActive";
                LoadData(Filter, SortBy, SortType);
            }
        }

        /// <summary> 
        /// Handles the Command event of the LinkButtonOrganizationName control. 
        /// </summary> 
        /// <param name="sender">The source of the event.</param> 
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonDIRName_Command(object sender, CommandEventArgs e)
        {
            new DirectorateBUS().ChangeDirectorateStatus(Convert.ToInt16(e.CommandName), true);
            Response.Redirect("EditDirectorate.aspx?directorateId=" + e.CommandName);
        }

        /// <summary>
        /// if isactive = false then show dialog to ask user want to update isactive = true, and then redirect to editdirectorate
        /// else redirect to editrectorate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewDirectorate_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].HasControls())
                {
                    LinkButton LinkButtonPROName = ((LinkButton)e.Row.Cells[0].Controls[1]);
                    var directorateView = e.Row.DataItem as SD.DataAccess.StoredDirectorateResult;
                    if (directorateView.IsActive == false)
                        LinkButtonPROName.Attributes.Add("onclick",
                            "if(confirm(\"Do you want to make this Directorate active?\") == true)"
                            + "{return true;} else {window.location.href='EditDirectorate.aspx?directorateId= " + directorateView.DirectorateId + "'; return false;}");
                }
            }
        }

        /// <summary>
        /// go to adddirectorate
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            int orgId = 0;
            if (int.TryParse((Parent.FindControl("HiddenFieldOrganizationId") as HiddenField).Value, out orgId))
            {
                Response.Redirect("AddDirectorate.aspx?OrgID=" + orgId);
            }
        }
    }
}